<template>
  <div class="wrapper">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入搜索内容"
            class="input-with-select"
            clearable
            v-model="goodParams.query"
            @change="inputChange"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button
            type="primary"
            @click="addGoodFirstClick"
          >添加商品</el-button>
        </el-col>
      </el-row>

      <template>
        <!-- 表格 -->
        <el-table
          :data="tableData"
          style="width: 100%"
          border
          stripe
        >
          <el-table-column
            type="index"
            label="#"
          > </el-table-column>
          <el-table-column
            prop="goods_name"
            label="商品名称"
          > </el-table-column>
          <el-table-column
            prop="goods_price"
            label="商品价格（远）"
          > </el-table-column>
          <el-table-column
            prop="goods_weight"
            label="商品重量"
          > </el-table-column>
          <el-table-column label="创建时间"><template slot-scope="scope">{{ (Number(scope.row.upd_time)*1000)|formatDate }}</template></el-table-column>
          <el-table-column
            label="操作"
            width="200px"
          >
            <template slot-scope="scope">
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                @click="onEdit(scope.row)"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="onDelete(scope.row)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="goodParams.pagenum"
        :page-sizes="[5, 10, 20, 25]"
        :page-size="100"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data () {
    return {
      tableData: [],
      goodParams: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      total: 0
    }
  },
  created () {
    this.getGoodsList();
  },
  methods: {
    async getGoodsList () {
      const { data: res } = await this.$http.get('goods', { params: this.goodParams });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.tableData = res.data.goods;
      this.total = res.data.total;
    },
    async deleteGood (data) {
      const { data: res } = await this.$http.delete(`goods/${data.goods_id}`);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.$message.success(res.meta.msg);
      this.getGoodsList();
    },
    inputChange () {
      this.getGoodsList();
    },
    addGoodFirstClick () {
      console.log(this);
      this.$router.push('/addgoods');
    },
    onEdit (data) {

    },
    onDelete (data) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteGood(data)
      });
    },
    handleSizeChange (pagesize) {
      this.goodParams.pagesize = pagesize;
      this.getGoodsList();
    },
    handleCurrentChange (page) {
      this.goodParams.pagenum = page;
      this.getGoodsList();
    }
  }
}
</script>
<style lang="sass" scoped>
</style>
